package com.homework.dao;

import com.homework.model.User;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.*;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/17 23:35
 */

@Repository
public class UserDao {
    // 数据库连接信息
    private final String jdbcUrl = "jdbc:mysql://localhost:3306/javaee_user";
    private final String jdbcUsername = "root";
    private final String jdbcPassword = "123456";

    public int insert(User user) {
        // 预期返回的插入记录数
        int result = 0;

        // JDBC 连接对象
        try (Connection connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)) {
            // SQL 插入语句
            String sql = "INSERT INTO tb_user (user_id, name, password, salt) VALUES (?, ?, ?, ?)";
            // 创建 PreparedStatement
            try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                preparedStatement.setInt(1, user.getUserId());
                preparedStatement.setString(2, user.getName());
                preparedStatement.setString(3, user.getPassword());
                preparedStatement.setString(4, user.getSalt());

                // 执行插入操作，并返回插入的记录数
                result = preparedStatement.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * todo: 实现登录
     * @param name
     * @param password
     * @return
     */
    public User login(String name, String password) {

        User user = null;
        String sql = "SELECT * FROM tb_user WHERE name = ? AND password = ?";

        try (Connection connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, name);
            preparedStatement.setString(2, password);

            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    // 假设User类有相应的构造函数或者setter方法来设置属性
                    user = new User();
                    user.setUserId(resultSet.getInt("user_id"));
                    user.setName(resultSet.getString("name"));
                    user.setPassword(resultSet.getString("password"));
                    user.setSalt(resultSet.getString("salt"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * todo: 统计所有的用户数
     * @return
     */
    public int countAllUsers() {

        int count = 0;
        String sql = "SELECT COUNT(*) AS total FROM tb_user";

        try (Connection connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            if (resultSet.next()) {
                count = resultSet.getInt("total");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }
    }


